package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.BuyNum;
import com.markerhub.entity.GoodsInfo;
import com.markerhub.entity.SysGoods;
import com.markerhub.entity.SysUser;
import com.markerhub.service.GoodsInfoService;
import com.markerhub.service.SysGoodsService;
import com.markerhub.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * @author jsx on 2023/2/21
 **/
@RestController
@RequestMapping("/goods/info")
public class GoodsInfoController extends BaseController{

    @Autowired
    private GoodsInfoService goodsInfoService;
    @Autowired
    private SysGoodsService sysGoodsService;

    //增加购物车
    @PostMapping("/addCar")
    public Result addCar(@RequestBody GoodsInfo goodsInfo, Principal principal){
        //判断是否已经在购物车中,
        SysUser user = sysUserService.getByUsername(principal.getName());
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("status","0").eq("user_id",user.getId().toString()).eq("goods_id",goodsInfo.getGoodsId());
        List<GoodsInfo> list = goodsInfoService.list(wrapper);
        if(list.size()>0){
            return Result.succ("你已经加入过购物车!");
        }
        goodsInfo.setStatus("0");
        goodsInfo.setNum(0);
        goodsInfo.setUserId(user.getId().toString());
        goodsInfo.setCreateTime(DateUtils.getTime(new Date()));
        goodsInfo.setDeleteFlag("0");
        goodsInfoService.save(goodsInfo);
        return Result.succ("加入完成");
    }
    //增加商品
    @PostMapping("/addInfo")
    public Result addInfo(@RequestBody GoodsInfo goodsInfo,Principal principal){
        if(goodsInfo.getNum() <= 0){
            return Result.fail("购买失败，至少购买1件商品");
        }
        goodsInfo.setStatus("1");
        SysUser user = sysUserService.getByUsername(principal.getName());
        goodsInfo.setUserId(user.getId().toString());
        goodsInfo.setCreateTime(DateUtils.getTime(new Date()));
        goodsInfo.setDeleteFlag("0");
        goodsInfo.setLogisticsStatus("等待收货");
        //实际上还要有判断价格问题
        goodsInfoService.save(goodsInfo);
        return Result.succ("购买完成");
    }

    //修改为购物商品

    @PostMapping ("/updateCar")
    Result updateCar(@RequestBody GoodsInfo goodsInfo,Principal principal){
        if(goodsInfo.getNum() <= 0){
            return Result.fail("购买失败，至少购买1件商品");
        }
        goodsInfo.setStatus("1");
        goodsInfo.setCreateTime(DateUtils.getTime(new Date()));
        SysUser user = sysUserService.getByUsername(principal.getName());
        goodsInfo.setUserId(user.getId().toString());
        goodsInfo.setLogisticsStatus("等待收货");
        goodsInfoService.updateById(goodsInfo);
        return Result.succ("购买完成");
    }

    //按状态查询购物车/购买商品
    @GetMapping("/list")
    Result search(String status, String name,Principal principal,String reject){
        SysUser user = sysUserService.getByUsername(principal.getName());
        List<SysGoods> list = sysGoodsService.getListByStatus(status, name, user.getId().toString(),reject);
        return Result.succ(list);
    }

    //删除购物车
    @GetMapping("/deleteCar")
    Result deleteCar(String id) {
        goodsInfoService.removeById(id);
        return Result.succ("删除成功");
    }

    //删除商品
    @PostMapping("/deleteInfo")
    Result deleteInfo(@RequestBody  String[] ids){
        if(ids.length == 0 || ids == null){
            return  Result.fail("请选择要删除的记录");
        }
        int res = goodsInfoService.deleteInfoList(ids);
        if(res < 0){
            return Result.fail("只能删除已收货或者已退货的信息");
        }
        return Result.succ("删除成功");
    }

    //统计前10数据
    @GetMapping("/statistics")
    Result statistics(){
        List<BuyNum> buyNums = goodsInfoService.countMaxTenNum();
        return Result.succ(buyNums);
    }

    @GetMapping("/confirm")
    Result confirm(String id){
        GoodsInfo goodsInfo = goodsInfoService.getById(id);
        goodsInfo.setLogisticsStatus("已收货");
        goodsInfoService.updateById(goodsInfo);
        return Result.succ("收货成功");
    }

    @GetMapping("/reject")
    Result reject(String id){
        GoodsInfo goodsInfo = goodsInfoService.getById(id);
        goodsInfo.setLogisticsStatus("申请退款");
        goodsInfoService.updateById(goodsInfo);
        return Result.succ("收货成功");
    }

    @GetMapping("/agree")
    Result agree(String id){
        GoodsInfo goodsInfo = goodsInfoService.getById(id);
        goodsInfo.setLogisticsStatus("已退货");
        goodsInfoService.updateById(goodsInfo);
        return Result.succ("退货成功");
    }

    @GetMapping("/reject1")
    Result reject1(String id){
        GoodsInfo goodsInfo = goodsInfoService.getById(id);
        goodsInfo.setLogisticsStatus("等待收货");
        goodsInfoService.updateById(goodsInfo);
        return Result.succ("操作完成");
    }


}
